package com.future.controller;

import com.future.domain.Article;
import com.future.domain.DTO.ArticleDTO;
import com.future.domain.Result;
import com.future.service.ArticleService;

import com.future.allUtils.constants.PermissionConstants;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 发布文章接口
     */
    @PreAuthorize("hasAnyAuthority('" +PermissionConstants.STUDENT_ARTICLE_PUBLISH + "', '" + PermissionConstants.TEACHER_ARTICLE_PUBLISH + "', '" + PermissionConstants.PSYCHOLOGIST_ARTICLE_PUBLISH + "')")
    @PostMapping("/publish")
    public Result publishArticle(@RequestBody ArticleDTO articleDTO) {
        return articleService.publishArticle(articleDTO);
    }
    /**
     * 审核文章接口
     */
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.TEACHER_ARTICLE_REVIEW + "', '" + PermissionConstants.ADMIN_ARTICLE_REVIEW + "')")
    @PostMapping("/audit/{articleId}")
    public Result auditArticle(@PathVariable Long articleId, @RequestParam Integer isApproved) {
        return articleService.auditArticle(articleId, isApproved);
    }
    /**
     * 根据文章ID获取文章详情
     */
    @GetMapping("/{articleId}")
    public Result getArticleByArticleId(@PathVariable Long articleId) {
        return articleService.getArticleByArticleId(articleId);
    }
    /**
     * 获取已审核文章列表（分页）
     */
    @GetMapping("/approved")
    public Result getApprovedArticles(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        return articleService.getApprovedArticles(page);
    }
    /**
     * 获取待审核文章列表（分页）
     */
    @GetMapping("/audit")
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.TEACHER_ARTICLE_REVIEW + "', '"
            + PermissionConstants.ADMIN_ARTICLE_REVIEW + "', '"
            + PermissionConstants.PSYCHOLOGIST_ARTICLE_PUBLISH + "')")
    public Result getAuditArticles(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        return articleService.getAuditArticles(page);
    }
    /**
     * 根据分类名字获取已审核文章列表（分页）
     */
    @GetMapping("/category")
    public Result getArticlesByCategoryName(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String categoryName) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        return articleService.getArticlesByCategoryName(page, categoryName);
    }
    /**
     * 根据标题获取已审核文章列表（分页）
     */

    @GetMapping("/title")
    public Result getArticlesByTitle(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String title) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        return articleService.getArticlesByTitle(page, title);
    }
    /**
     * 根据分类ID获取已审核文章列表（分页）
     */
    @GetMapping("/category/{categoryId}")
    public Result getArticlesByCategoryId(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @PathVariable Long categoryId) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        return articleService.getArticlesByCategoryId(page, categoryId);
    }
    /**
     * 根据作者ID获取文章详情
     */
    @GetMapping("/author/{authorId}")
    public Result getArticleByAuthorId( @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        @PathVariable Long authorId){
        return articleService.getArticleByAuthorId(authorId, pageNum, pageSize);
    }
    /**
     * 获取本人文章详情
     */
    @GetMapping("/self")
    public Result getSelfArticle( @RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize){
        return articleService.getSelfArticle(pageNum, pageSize);
    }
    /**
     * 删除本人文章
     */
    @DeleteMapping("/self/{articleId}")
    public Result deleteSelfArticle(@PathVariable Long articleId) {
        return articleService.deleteSelfArticle(articleId);
    }
    /**
     * 管理员删除文章
     */
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_ARTICLE_REVIEW + "')")
    @DeleteMapping("/admin/{articleId}")
    public Result deleteArticle(@PathVariable Long articleId) {
        return articleService.deleteArticle(articleId);
    }
    /**
     * 文章点赞接口
     */
    @PostMapping("/like/{articleId}")
    public Result likeArticle(@PathVariable Long articleId) {
        return articleService.getArticleLikeCount(articleId);
    }
    /**
     * 根据作者ID和标题获取文章列表（分页）
     */
    @GetMapping("/author/{authorId}/title")
    public Result getArticlesByAuthorIdAndTitle(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @PathVariable Long authorId,
            @RequestParam String title) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        return articleService.getArticlesByAuthorIdAndTitle(page, authorId, title);
    }
    /**
     * 根据标题获取本人文章列表（分页）
     */
    @GetMapping("/author/title")
    public Result getArticlesByAuthorAndTitle(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String title) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        return articleService.getArticlesByAuthorAndTitle(page, title);
    }

    @ApiOperation("检查文章是否被点赞收藏过")
    @PostMapping("/check/{articleId}")
    public Result checkArticle(@PathVariable Long articleId) {
        return articleService.checkArticle(articleId);
    }
}
